﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Library;
using System.Drawing;

namespace Sport_Shop
{
    public partial class Giohang : System.Web.UI.Page
    {
        static DataTable tbGioHang = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_ListSanpham();
                get_ngay();
            }
        }

        private void load_ListSanpham()
        {
            try
            {
                if (Session["GioHang"] == null)
                {
                    Response.Redirect("~/Index.aspx");
                }
                tbGioHang = (DataTable)Session["GioHang"];
                

                GVListSanpham.DataSource = tbGioHang;
                GVListSanpham.DataBind();
                GVListSanpham.FooterRow.Cells[2].Text = "Tổng tiền";
                GVListSanpham.FooterRow.Cells[3].Text = int.Parse(tbGioHang.Compute("Sum(ThanhTien)", "").ToString()).ToString("#,###,###");
            }
            catch (Exception e)
            {
                Response.Redirect("~/Index.aspx");
            }
        }



        //Thêm hàng vào gridview gio hang
        /*
        public void Add_GioHang(string MaSP, string TenSP, int DonGia, int SoLuong)
        {
            DataTable dt;
            if (Session["GioHang"] == null)
            {
                dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("TenSP");
                dt.Columns.Add("DonGia");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("ThanhTien");

            }
            else
                dt = (DataTable)Session["GioHang"];

            //Tìm ID_SP dựa vào MaSP
            List<Library.SanPham> idsp = new List<Library.SanPham>();
            idsp = Library.SanPham.GetSPByMa(MaSP);

                DataRow dr = dt.NewRow();
                dr["ID"] = idsp[0].ID;
                dr["TenSP"] = TenSP;
                dr["DonGia"] = DonGia;
                dr["SoLuong"] = SoLuong;
                dr["ThanhTien"] = DonGia * SoLuong;
                dt.Rows.Add(dr);
            
            Session["GioHang"] = dt;
        }
        */

        //public static int SoLuongOfGioHang(string TenSP, DataTable dt)
        //{
        //    int dong = -1;
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        if (dt.Rows[i]["TenSP"].ToString() == TenSP)
        //        {
        //            dong = i;
        //            break;
        //        }
        //    }
        //    return dong;
        //}

        public void get_ngay()
        {
            string date = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
            tbNgay.Text = date;
            tbNgay.Enabled = false;
        }

        protected void GVListSanpham_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            tbGioHang = Session["GioHang"] as DataTable;
            tbGioHang.Rows.RemoveAt(e.RowIndex);
            GVListSanpham.DataSource = null;
            load_ListSanpham();
            
        }

        protected void btDelGH_Click(object sender, EventArgs e)
        {
            Session["GioHang"] = null;
            Response.Redirect("~/Index.aspx");
        }

        protected void btUpdateGH_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)Session["GioHang"];
                foreach (GridViewRow r in GVListSanpham.Rows)
                {
                    foreach (DataRow dr in dt.Rows)
                    {

                        if (GVListSanpham.DataKeys[r.RowIndex].Value.ToString().Trim() == dr["TenSP"].ToString().Trim())
                        {
                            TextBox t = (TextBox)r.Cells[2].FindControl("txtSoLuong");
                            if (Convert.ToInt32(t.Text) <= 0)
                                dt.Rows.Remove(dr);
                            else
                                dr["SoLuong"] = t.Text;
                           // break;
                        }   
                    }  
                }
                Session["Giohang"] = dt;

                GVListSanpham.DataSource = null;
                load_ListSanpham();
            }
            catch (Exception)
            {
                Response.Write("<script>alert('Đã Phát Sinh Lỗi, Vui Lòng Kiểm Tra Lại!...')</script>");
            }
        }

        protected void btSubmitGH_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["GioHang"] == null)
                {
                    Response.Redirect("~/GioHangRong.aspx");
                }

                //Kiểm tra số lượng hàng trong kho
                int dem = 0;
                foreach (DataRow r in tbGioHang.Rows)
                        {
                            //Lấy IDSP
                            int IDSP = int.Parse(r["ID"].ToString());

                            //Kiểm tra số lượng
                            if ((ChiTietKho.GetLastestSoLuongTon(IDSP) - int.Parse(r["SoLuong"].ToString())) >= 0)
                                dem++;
                            else
                            {
                                Label1.Text = r["TenSP"].ToString() + "đã hết hàng !!!";
                            }
                        }

                //Kiểm tra đã điền đủ thông tin chưa
                int dem1 = 0;
                if (tbTenKH.Text != "" && tbDiaChi.Text != "" && tbSoDT.Text != "" && tbEmail.Text != "")
                    dem1 = 1;
                else
                    lb1.ForeColor = Color.Red;

                if (dem == tbGioHang.Rows.Count && dem1 == 1)
                {
                    //Insert vào DonDatHang
                    DonDatHang dh = new DonDatHang(-1, DateTime.ParseExact(tbNgay.Text, "dd/MM/yyyy HH:mm", null),
                            tbTenKH.Text.ToString().Trim(),
                            tbDiaChi.Text.ToString().Trim(),
                            tbSoDT.Text.ToString().Trim(),
                            tbEmail.Text.ToString().Trim());

                    if (dh.MaDDH == -1)
                    {
                        dh.Add();
                    }

                    //Insert vào ChiTietDDH
                    if (tbGioHang.Rows.Count > 0)
                    {
                        foreach (DataRow r in tbGioHang.Rows)
                        {
                            //Lấy IDSP
                            int IDSP = int.Parse(r["ID"].ToString());

                            //Insert
                            ChiTietDDH ct = new ChiTietDDH(dh.MaDDH, IDSP, int.Parse(r["SoLuong"].ToString()));
                            ct.Add();
                            
                            //Trừ trong kho
                            ChiTietKho.SubKho(IDSP, int.Parse(r["SoLuong"].ToString()));
                        }
                    }

                    Session["GioHang"] = null;
                    dem = 0;
                    lb1.ForeColor = Color.White;
                    Response.Redirect("~/DHThanhCong.aspx");
                }
            }
            catch
            {
                Response.Write("<script>alert('Đã Phát Sinh Lỗi, Vui Lòng Kiểm Tra Lại!...')</script>");
            }
        }

    }
}